package cn.chenxiejia;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: T5
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/7 20:23
 */
public class T5 {
    public static void main(String[] args) {
        Testr rw = new Testr();
        new Thread(rw::set).start();
        new Thread(rw::get).start();
    }

    static class Testr {
        static double num = 0;
        static ReadWriteLock lock = new ReentrantReadWriteLock();

        public Testr() {
        }

        public void get() {
            lock.readLock().lock();
            for (int i = 0; i < 100; i++) {
                System.out.println(num);
            }
            lock.readLock().unlock();
        }

        public void set() {
            lock.writeLock().lock();
            num = Math.random() * 10;
            lock.writeLock().unlock();
        }
    }
}
